package com.ruoyi.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HistoricalSessions;
import com.ruoyi.system.service.IHistoricalSessionsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 历史会话Controller
 * 
 * @author ruoyi
 * @date 2025-01-09
 */
@RestController
@RequestMapping("/system/sessions")
public class HistoricalSessionsController extends BaseController
{
    @Autowired
    private IHistoricalSessionsService historicalSessionsService;

    /**
     * 查询历史会话列表
     */
    @PreAuthorize("@ss.hasPermi('system:sessions:list')")
    @GetMapping("/list")
    public TableDataInfo list(HistoricalSessions historicalSessions)
    {
        startPage();
        // 设置当前用户ID
        historicalSessions.setUserId(SecurityUtils.getUserId());
        List<HistoricalSessions> list = historicalSessionsService.selectHistoricalSessionsList(historicalSessions);
        return getDataTable(list);
    }

    /**
     * 导出历史会话列表
     */
    @PreAuthorize("@ss.hasPermi('system:sessions:export')")
    @Log(title = "历史会话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HistoricalSessions historicalSessions)
    {
        historicalSessions.setUserId(SecurityUtils.getUserId());
        List<HistoricalSessions> list = historicalSessionsService.selectHistoricalSessionsList(historicalSessions);
        ExcelUtil<HistoricalSessions> util = new ExcelUtil<HistoricalSessions>(HistoricalSessions.class);
        util.exportExcel(response, list, "历史会话数据");
    }

    /**
     * 获取历史会话详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sessions:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        HistoricalSessions sessions = historicalSessionsService.selectHistoricalSessionsById(id);
        if (sessions != null && !sessions.getUserId().equals(SecurityUtils.getUserId()))
        {
            return error("没有权限访问该会话");
        }
        return success(sessions);
    }

    /**
     * 新增历史会话
     */
    @PreAuthorize("@ss.hasPermi('system:sessions:add')")
    @Log(title = "历史会话", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HistoricalSessions historicalSessions)
    {
        historicalSessions.setUserId(SecurityUtils.getUserId());
        return toAjax(historicalSessionsService.insertHistoricalSessions(historicalSessions));
    }

    /**
     * 修改历史会话
     */
    @PreAuthorize("@ss.hasPermi('system:sessions:edit')")
    @Log(title = "历史会话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HistoricalSessions historicalSessions)
    {
        HistoricalSessions original = historicalSessionsService.selectHistoricalSessionsById(historicalSessions.getId());
        if (original != null && !original.getUserId().equals(SecurityUtils.getUserId()))
        {
            return error("没有权限修改该会话");
        }
        return toAjax(historicalSessionsService.updateHistoricalSessions(historicalSessions));
    }

    /**
     * 删除历史会话
     */
    @PreAuthorize("@ss.hasPermi('system:sessions:remove')")
    @Log(title = "历史会话", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        // 验证所有要删除的会话是否属于当前用户
        for (Long id : ids)
        {
            HistoricalSessions sessions = historicalSessionsService.selectHistoricalSessionsById(id);
            if (sessions != null && !sessions.getUserId().equals(SecurityUtils.getUserId()))
            {
                return error("没有权限删除ID为" + id + "的会话");
            }
        }
        return toAjax(historicalSessionsService.deleteHistoricalSessionsByIds(ids));
    }
}
